package com.cnhis.cloudhealth.clinical.util;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import com.google.common.cache.Cache;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Maps;

public class CacheUtil {
	
 
	public static Map cacheMap_index = Maps.newConcurrentMap();
	
	
	LoadingCache LoadingCache1 = CacheBuilder.newBuilder()
			.maximumSize(100000).expireAfterAccess(1, TimeUnit.MINUTES)

			.build(

			new CacheLoader() {

				@Override
				public Object load(Object arg0) throws Exception {
					 
					return null;
				}

			});
			
	public static Cache LoadingCache1_4cfgload = CacheBuilder.newBuilder()
			.maximumSize(100000).expireAfterAccess(15, TimeUnit.SECONDS)
			.build( );
	
	
	public static void main(String[] args) throws CantfindEx {
		CacheUtil.setValInCache("k", "parameter");
		System.out.println(getValFromCache("k"));
	}

	public static Object getValFromCache(Object cacheKey) throws CantfindEx {

		Map findCacheRztMap = (Map) cacheMap_index.get(cacheKey);
		if (findCacheRztMap != null) {
			Long uptime = (Long) findCacheRztMap.get("uptime");
			Long now = System.currentTimeMillis();
			long spantime = now - uptime;
			if (spantime < 600 * 1000) // 60s
			{
				return  findCacheRztMap.get("val");
			}

		}
		throw new CantfindEx();
	}

	public static void setValInCache(Object cacheKey, Object parameter) {
		Object val = parameter;
		Long now = System.currentTimeMillis();

		HashMap mapVal = new HashMap();
		mapVal.put("key", cacheKey);
		mapVal.put("val", val);
		mapVal.put("uptime", now);
		cacheMap_index.put(cacheKey, mapVal);

	}

}
